<%@ page contentType="text/html;charset=utf-8" pageEncoding="UTF-8"%>
<%@include file="/common/common.jsp" %>

<html>
	<head>
		<title>查询区域</title>
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/skin.css">
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/dhtmlxtree/dhtmlxtree.css">
		<link href="${pageContext.request.contextPath}/css/jquery.alerts.css" rel="stylesheet" type="text/css" media="screen">		
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery/jquery-1.4.2.min.js"></script>
		<script src="${pageContext.request.contextPath}/js/jquery/jquery.ui.draggable.js" type="text/javascript"></script>
		<script src="${pageContext.request.contextPath}/js/jquery/jquery.alerts.js" type="text/javascript"></script>
		<script src="${pageContext.request.contextPath}/js/dhtmlxtree/dhtmlxcommon.js"  type="text/javascript"></script>
		<script src="${pageContext.request.contextPath}/js/dhtmlxtree/dhtmlxtree.js"  type="text/javascript"></script>
		<script type="text/javascript">
			var selectedId = "";
			var selectedValue = "";
						
			function onClose(){
				if(selectedValue != ""){
					jConfirm('确定选中组织机构：' +  selectedValue + "?", '操作确认', function(r) {
						if(r){
							parent.setElementsValue("group", selectedId, selectedValue);
	                    	parent.ClosePop();
						}
	                });
				}else
					parent.ClosePop();
			}

			//获取界面初始化值
			function init(){
				groupForm.employeeType.value = parent.getElementsValue("employeeType");
				groupForm.groupCode.value = parent.getElementsValue("groupCode");
				
				if(groupForm.employeeType.value == 2){
					groupForm.originalGroupId.value = parent.getElementsValue("originalGroupId");
					groupForm.originalGroupName.value = parent.getElementsValue("originalGroupName"); 
				}
			}

			function onSearch(){
				var url = "searchArea.action";
				var params = {
						queryAreaName:$('#queryAreaName').val()
				};
				$.post(url, params, callback, "text");
			}

			var tempId = "";
			var tempName = "";
			var tempCode = "";

			function callbackRoot(data){
				alert("call");
				tempId = "";
				tempName = "";
				tempCode = "";
							
				if(data == "success"){					
					//查询成功
					//tempId = data.treeNode.split(";")[0];
					//tempName = data.treeNode.split(";")[1];
					//tempCode = data.treeNode.split(";")[2];
					//alert(tempId);
					alert(tempCode);
					
					//加载树
					var url = "searchArea.action";
					
					var params = {
							rootGroupId:tempId,
							rootGroupName:tempName,
							groupCode:tempCode
						};
					$.post(url, null, callback, "text");	
					
				}else
					jAlert('error', data.results, '操作提示');
			}
			
			
			function callback(d){	
				tree.deleteChildItems(0);
					            
	            tree.loadXMLString(d);
				
			}			
		</script>
		
	</head>
	
	<body >
		<s:form name="memberForm" id="memberForm" method="post">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr>
			     <td width="3">&nbsp;</td>
			  	 <td><table width="100%" height="30" border="0" bgcolor="b5d6e6" cellpadding="0" cellspacing="0" class="nowtable">
              	 	 <tr>
                 		<td width="15%" height="30" bgcolor="#f2f2f2" class="left_txt2">&nbsp;&nbsp;区域名称：</td>
                		<td width="23%" bgcolor="#f2f2f2"><input name="queryAreaName" id="queryAreaName" type="text" size="18" maxlength="50"></td>
                		<td width="24%" align="right"  bgcolor="#f2f2f2"><input type="button" name="sb1" value="查询"  onclick="onSearch()" /> &nbsp;&nbsp;
                			<input type="button" name="rb1" value="清空"  onclick="onReset()" />
                		</td>
              		 </tr>
        			</table>
        		</td>
        	</tr>
        </table>        
		<input type="hidden" name="employeeType" id="employeeType" />
		<input type="hidden" name="originalGroupId" id="originalGroupId" />
        <input type="hidden" name="originalGroupName" id="originalGroupName" />
        <input type="hidden" name="rootGroupId" id="rootGroupId" />
        <input type="hidden" name="groupCode" id="groupCode" />        
        </s:form>
      
        <table>
        	<tr>
        		<td>
        			<div id="treebox_tree" style="width:500px; height:218px;background-color:#f5f5f5;border :1px solid Silver; "></div>        			
        		</td>
        	</tr>
        </table>
        
        <script type="text/javascript">
        	tree = new dhtmlXTreeObject("treebox_tree", "100%", "100%", 0);
        
        	tree.setSkin('dhx_skyblue');
        	tree.setImagePath("${pageContext.request.contextPath}/css/dhtmlxtree/imgs/csh_bluebooks/");
        	tree.enableCheckBoxes(1);
        	//设置单击事件
        	tree.setOnClickHandler(addChild);
        	//设置复选框选中事件
        	tree.setOnCheckHandler(test);
        	//设置展开事件
        	tree.setOnOpenStartHandler(addChild);
        	   	
        	//tree.loadXML("tree3.xml");
			var loadParentId;
        	//重新加载子结点
        	function addChild(id){
            	var ids = id.split(";");
            	if(ids[2] === "true"){
                	var nodeId = tree.getSubItems(id);
                	if(typeof(nodeId)=='undefined' || nodeId.length == 0 || 
                       nodeId.indexOf("loaded") == -1){
                		loadParentId = id;
                		alert("准备加载");
                		//1.删除已有节点                	                	
                		//2.加载节点
                		var url = "loadArea.action";
                		var params = {
                			loadAreaId:ids[0]
                		};
                	
                		$.post(url, params, onLoadSubArea, "json");
                	}else{
                    	//判断是展开还是合拢
                    	var count = tree.getOpenState(id);
                    	if(count == -1){
                        	return true;
                        	//alert("展开");
                        	//tree.openItem(id);
                    	}else if(count == 1){
                    		
                    		tree.closeItem(id);
                    	}
                	}
                	
            	}
        	}

        	function onLoadSubArea(data){
        		alert(data.rows.length);
        		var len = data.rows.length;
            	if(len > 0){
        			tree.deleteChildItems(loadParentId);
        			for(var i = 0; i < len; i++){
        				 tree.insertNewChild(loadParentId, data.rows[i].id + ";loaded", 
        						 data.rows[i].text,'',data.rows[i].im0,
        						 data.rows[i].im1,data.rows[i].im2,'',data.rows[i].child);
        				 //alert(data.rows[i].child);
        				 if(data.rows[i].child){
							// alert("eee");
							 tree.insertNewChild(data.rows[i].id + ";loaded", data.rows[i].id, 
	        						 data.rows[i].text,'',data.rows[i].im0,
	        						 data.rows[i].im1,data.rows[i].im2,'','');
						 }
        				 tree.closeItem(data.rows[i].id + ";loaded");
        			}
        		//alert(data);
        		
        			//tree.openAllItems(loadParentId);
            	}
            	loadParentId = "";
        	}
        	
        	function test(id){
            	alert(id);
        	}
        </script>
        <br />
		<input type="button" name="ss" value="确定" onclick="onClose();">		
	</body>
</html>